<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-tabs>
        <!-- 放置页签 -->
        <el-tab-pane label="角色管理">
          <!-- 新增角色按钮 -->
          <el-row style="height:60px">
            <el-button icon="el-icon-plus" size="small" type="primary" @click="dialogVisible = true">新增角色</el-button>
          </el-row>
          <!-- 表格 -->
          <el-table border="" :data="rolesList">
            <el-table-column label="序号" width="120" type="index" />
            <el-table-column label="角色名称" width="240" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button size="small" type="success" @click="assignPerm(scope.row.id)">分配权限</el-button>
                <el-button size="small" type="primary" @click="editRole(scope.$index, scope.row)">编辑</el-button>
                <el-button size="small" type="danger" @click="delRole(scope.$index, scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页组件 -->
          <el-pagination
            :current-page.sync="page.page"
            :page-size="page.pagesize"
            :total="page.total"
            layout="prev,pager,next"
            @current-change="getRoleList"
          />
        </el-tab-pane>
        <el-tab-pane label="公司信息">
          <el-tabs>
            <el-tab-pane label="公司信息">
              <el-alert
                title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
                type="info"
                show-icon
                :closable="false"
              />
              <el-form label-width="120px" style="margin-top:50px">
                <el-form-item label="公司名称">
                  <el-input v-model="formData.name" disabled style="width:400px" />
                </el-form-item>
                <el-form-item label="公司地址">
                  <el-input v-model="formData.companyAddress" disabled style="width:400px" />
                </el-form-item>
                <el-form-item label="邮箱">
                  <el-input v-model="formData.mailbox" disabled style="width:400px" />
                </el-form-item>
                <el-form-item label="备注">
                  <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
                </el-form-item>
              </el-form>
            </el-tab-pane>
          </el-tabs>
        </el-tab-pane>
      </el-tabs>
    </div>
    <el-dialog
      :title="roleForm.id? '编辑':'新增'"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="beforeClose"
    >
      <el-form ref="form" label-width="80px" :model="roleForm" :rules="roleFormRules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="beforeClose">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
      <!-- 权限是一颗树 -->
      <!-- 将数据绑定到组件上 -->
      <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
      <!-- id作为唯一标识 -->
      <el-tree
        ref="permTree"
        :data="permData"
        :props="defaultProps"
        :show-checkbox="true"
        :check-strictly="true"
        :default-expand-all="true"
        :default-checked-keys="selectCheck"
        node-key="id"
      />
      <!-- 确定 取消 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole, getCompanyInfo, updateRole, assignPerm, getRoleDetail } from '@/api/setting'
import { tranListToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permission'
export default {
  data() {
    return {
      page: {
        pagesize: 10,
        page: 1,
        total: 0
      },
      rolesList: [],
      dialogVisible: false,
      roleForm: {
        name: '',
        description: ''
      },
      roleFormRules: {
        name: {
          required: true,
          message: '请填写角色名称',
          trigger: 'blur'
        },
        description: {
          required: true,
          message: '请填写角色描述',
          trigger: 'blur'
        }
      },
      formData: {},
      defaultProps: {
        label: 'name'
      },
      showPermDialog: false,
      permData: [],
      selectCheck: [],
      roleId: ''
    }
  },

  async created() {
    this.getRoleList()
    this.getCompanyInfo()
  },

  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.rolesList = rows
      this.page.total = total
    },
    async delRole(i, r) {
      try {
        await this.$confirm('真的要删除？')
        await deleteRole(r.id)
        this.$message.success('删除成功')
        this.getRoleList()
      } catch (e) {
        console.log(e)
      }
    },
    async getCompanyInfo() {
      const res = await getCompanyInfo(this.$store.state.user.userInfo.companyId)
      this.formData = res
    },
    beforeClose() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    editRole(i, { id, name, description }) {
      this.dialogVisible = true
      this.roleForm = {
        name: name,
        description: description,
        id
      }
    },
    async submit() {
      await this.$refs.form.validate()
      if (this.roleForm.id) {
        await updateRole(this.roleForm)
        this.$message.success('修改成功')
      } else {
        await addRole(this.roleForm)
        this.$message.success('添加成功')
      }
      this.getRoleList()
      this.beforeClose()
    },
    btnPermCancel() {
      this.selectCheck = []
      this.showPermDialog = false
    },
    async btnPermOK() {
      await assignPerm({
        id: this.roleId,
        perIds: this.$refs.permTree.getCheckedKeys()
      })
      this.$message.success('分配成功')
      this.btnPermCancel()
    },
    async assignPerm(id) {
      this.showPermDialog = true
      this.permData = tranListToTreeData(await getPermissionList(), '0') // 转化list到树形数据
      this.roleId = id
      const { permIds } = await getRoleDetail(id) // permIds是当前角色所拥有的权限点数据
      this.selectCheck = permIds // 将当前角色所拥有的权限id赋值
    }
  }
}
</script>

<style scoped lang="less"></style>
